package com.mytools.tool.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class LocalParasUtils {

	/* ----------------以下是公开方法，用以获取当前收钱吧当前激活状态---------------------------- */
	private static String APP_SHARE_FLAG_FILENAME = "app_share_flag";
	
	private static String SQB_SHARE_FLAG_IS_ACTIVATED = "sqb_is_activated";
	private static String SQB_SHARE_ACTIVATED_CODE = "sqb_activate_code";
	private static String SQB_SHARE_MERCHANT_NO = "sqb_merchant_no";
	private static String SQB_SHARE_MERCHANT_NAME = "sqb_merchant_name";
	private static String SQB_SHARE_TERMINAL_NAME = "sqb_terminal_name";
	
	private static String LOGIN_X_DATE = "login_x_date";
	
	/**
	 * 设置本地登录时间
	 * @param context
	 * @param merchantNo
	 */
	public static void saveXLoginDate(Context context){
		String loginDate = TimeUtil.getCurrentDateString("yyyyMMdd");
		setStringPara(context, LOGIN_X_DATE, loginDate);
	}
	
	/**
	 * 获得最后一次登录时间
	 * @param context
	 * @return
	 */
	public static String getXLoginDate(Context context){
		return getStringPara(context, LOGIN_X_DATE, "");
	}
	
	/**
	 * 是否已经激活(收钱吧)
	 * @param context
	 * @return
	 */
	public static boolean checkActivateStatus(Context context){
		return getBooleanPara(context, SQB_SHARE_FLAG_IS_ACTIVATED, false);
	}
	
	/**
	 * 获得收钱吧激活码
	 * @param context
	 * @return
	 */
	public static String getActivateCode(Context context){
		return getStringPara(context, SQB_SHARE_ACTIVATED_CODE, "");
	}
	
	/**
	 * 设置收钱吧激活码
	 * @param context
	 * @param activiteCode
	 */
	public static void saveActivateCode(Context context, String activiteCode){
		setStringPara(context, SQB_SHARE_ACTIVATED_CODE, activiteCode);
		setBooleanPara(context, SQB_SHARE_FLAG_IS_ACTIVATED, true);
	}
	
	/**
	 * 设置商户号
	 * @param context
	 * @param merchantNo
	 */
	public static void saveMerchantNo(Context context, String merchantNo){
		setStringPara(context, SQB_SHARE_MERCHANT_NO, merchantNo);
	}
	
	/**
	 * 获得商户号
	 * @param context
	 * @return
	 */
	public static String getMerchantNo(Context context){
		return getStringPara(context, SQB_SHARE_MERCHANT_NO, "");
	}
	
	/**
	 * 设置商户名称
	 * @param context
	 * @param merchantName
	 */
	public static void saveMerchantName(Context context, String merchantName){
		setStringPara(context, SQB_SHARE_MERCHANT_NAME, merchantName);
	}
	
	/**
	 * 获得商户名称
	 * @param context
	 * @return
	 */
	public static String getMerchantName(Context context){
		return getStringPara(context, SQB_SHARE_MERCHANT_NAME, "");
	}
	
	/**
	 * 设置终端名称
	 * @param context
	 * @param merchantName
	 */
	public static void saveTerminalName(Context context, String merchantName){
		setStringPara(context, SQB_SHARE_TERMINAL_NAME, merchantName);
	}
	
	/**
	 * 获得终端名称
	 * @param context
	 * @return
	 */
	public static String getTerminalName(Context context){
		return getStringPara(context, SQB_SHARE_TERMINAL_NAME, "");
	}
	
	private static void setStringPara(Context context, String key, String value){
		SharedPreferences tempSharePreferences = context.getSharedPreferences(APP_SHARE_FLAG_FILENAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor tEditor = tempSharePreferences.edit();
		tEditor.putString(key, value);
		tEditor.commit();
	}
	
	private static String getStringPara(Context context, String key, String defaultValue){
		SharedPreferences sharePreferences = context.getSharedPreferences(APP_SHARE_FLAG_FILENAME, Context.MODE_PRIVATE);
		String code = sharePreferences.getString(key, defaultValue);
		return code;
	}
	
	private static void setBooleanPara(Context context, String key, boolean value){
		SharedPreferences tempSharePreferences = context.getSharedPreferences(APP_SHARE_FLAG_FILENAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor tEditor = tempSharePreferences.edit();
		tEditor.putBoolean(key, value);
		tEditor.commit();
	}
	
	private static Boolean getBooleanPara(Context context, String key, boolean defaultValue){
		SharedPreferences sharePreferences = context.getSharedPreferences(APP_SHARE_FLAG_FILENAME, Context.MODE_PRIVATE);
		Boolean code = sharePreferences.getBoolean(key, defaultValue);
		return code;
	}
	
	public static void removeActivateStatus(Context context){
		SharedPreferences tempSharePreferences = context.getSharedPreferences(APP_SHARE_FLAG_FILENAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor tEditor = tempSharePreferences.edit();
		tEditor.remove(SQB_SHARE_ACTIVATED_CODE);
		tEditor.remove(SQB_SHARE_FLAG_IS_ACTIVATED);
		tEditor.remove(SQB_SHARE_TERMINAL_NAME);
		tEditor.remove(SQB_SHARE_MERCHANT_NO);
		tEditor.commit();
	}
	
	/**在登录前清除掉上次登录时间标志，以免影响正常登陆*/
	public static void removeXLoginFlag(Context context){
		SharedPreferences tempSharePreferences = context.getSharedPreferences(APP_SHARE_FLAG_FILENAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor tEditor = tempSharePreferences.edit();
		tEditor.remove(LOGIN_X_DATE);
		tEditor.commit();
	}
	
}
